/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.table;

import javax.swing.event.TableModelListener;
import org.tiberiumlabs.lailaps.downloads.DownloadManager;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class TableModel implements javax.swing.table.TableModel, ColumnsNames {

    private final DownloadManager downloadManager;

    public TableModel(DownloadManager downloadManager) {
        this.downloadManager = downloadManager;
    }

    @Override
    public int getRowCount() {
        return downloadManager.getCount();
    }

    @Override
    public int getColumnCount() {
        return TableColumnsModel.columnsNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return TableColumnsModel.columnsNames[columnIndex];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case COL_STATUS: {
                return downloadManager.getStatus(rowIndex);
            }
            case COL_LINK: {
                return downloadManager.getLink(rowIndex);
            }
            case COL_FILE: {
                return downloadManager.getFile(rowIndex);
            }
            case COL_PATH: {
                return downloadManager.getPath(rowIndex);
            }
            case COL_SIZE: {
                return downloadManager.getSize(rowIndex);
            }
            case COL_COMPLETED: {
                return downloadManager.getCompleted(rowIndex);
            }
            case COL_SPEED: {
                return downloadManager.getSpeed(rowIndex);
            }
            case COL_PROGRESS: {
                return downloadManager.getProgress(rowIndex);
            }
            case COL_PERCENTS: {
                return downloadManager.getPercents(rowIndex);
            }
            case COL_TIME_LEFT: {
                return downloadManager.getTimeLeft(rowIndex);
            }
            case COL_TIME_SPENT: {
                return downloadManager.getTimeSpent(rowIndex);
            }
            case COL_SEGMENTS: {
                return downloadManager.getSegments(rowIndex);
            }
            case COL_SEGMENTS_PROGRESS: {
                return downloadManager.getSegmentsProgress(rowIndex);
            }
        }
        throw new RuntimeException("No suitable column found: " + columnIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return Object.class;
    }

    // <editor-fold defaultstate="collapsed" desc=" empty methods ">
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    }
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc=" table model listeners ">

    @Override
    public void addTableModelListener(TableModelListener l) {
        downloadManager.addTableModelListener(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        downloadManager.removeTableModelListener(l);
    }

    // </editor-fold>

}
